/*
 * Copyright 2013 Agustina Martinez-Garcia

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package uk.ljmu.qudexapps.manager.actions;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Locale;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.multipart.MultipartFile;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import uk.ljmu.qudexapps.manager.utilities.Constants;
import uk.ljmu.qudexapps.xmllib.utilities.IngestionResults;

public class EmailActions {

	@Autowired
	private JavaMailSender mailSender;

	@Autowired
	private TemplateEngine templateEngine;

	@Autowired
	private MessageSource messageSource;

	/*
	 * Send HTML mail with attachment.
	 */
	public String sendConfirmationEmail(final String recipientEmail,
			final String messageResults,
			final List<IngestionResults> ingestionResults,
			final String attachmentFileName, final MultipartFile attachment,
			final String attachmentContentType, final Locale locale, final String tempDir)
			throws MessagingException, IOException {
		String fullPath = "";
		if (attachment != null && attachment.getSize() > 0) {
		    fullPath = (tempDir.endsWith(System
			    .getProperty("file.separator"))) ? tempDir
			    + attachment.getOriginalFilename() : tempDir
			    + System.getProperty("file.separator")
			    + attachment.getOriginalFilename();
		}
		// Prepare the evaluation context
		final Context ctx = new Context(locale);
		ctx.setVariable("messageResults", messageResults);
		ctx.setVariable("results", ingestionResults);

		// Prepare message using a Spring helper
		final MimeMessage mimeMessage = this.mailSender.createMimeMessage();
		final MimeMessageHelper message = new MimeMessageHelper(mimeMessage,
				true /* multipart */, "UTF-8");
		String subject = messageSource
				.getMessage("email.subject", null, locale);
		message.setSubject(subject);
		message.setFrom(Constants.EMAIL_DEFAULT_ADDRESS);
		message.setTo(recipientEmail);

		// Create the HTML body using Thymeleaf
		final String htmlContent = this.templateEngine.process(
				"email-template.html", ctx);
		message.setText(htmlContent, true /* isHtml */);

		if (attachment != null && attachment.getSize() > 0) {
		    File file = new File(fullPath);
		    //writeToFile(attachment.getInputStream(), file);
		    FileSystemResource fileS = new FileSystemResource(file);
		    message.addAttachment(attachment.getOriginalFilename(),
			    fileS);
		}
		// Send mail
		this.mailSender.send(mimeMessage);
		
		return "success";

	}
}
